package com.csr.csrcheck.controller;

import com.csr.csrcheck.controller.ex.AppletIOException;
import com.csr.csrcheck.controller.ex.CompanyException;
import com.csr.csrcheck.pojo.Approvals;
import com.csr.csrcheck.pojo.Approved_by;
import com.csr.csrcheck.pojo.Product;
import com.csr.csrcheck.service.Abnormal_productsService;
import com.csr.csrcheck.service.ApprovalsService;
import com.csr.csrcheck.service.Approved_byService;
import com.csr.csrcheck.service.ProductService;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping({"approvals"})
@RestController
public class ApprovalsController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.ApprovalsController.class);

    @Resource
    private ApprovalsService approvalsService;

    @Resource
    private Abnormal_productsService abnormal_productsService;

    @Resource
    private ProductService productService;

    @Resource
    private Approved_byService approved_byService;

    @GetMapping({"approvalslist"})
    public JsonResult<List<Approvals>> Flight_checklist(HttpServletResponse response) throws IOException, ServletException {
        List<Approvals> list = this.approvalsService.getApprovalslist();
        if (list == null)
            throw new AppletIOException("");
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @GetMapping({"page"})
    public JsonResult<Object> page(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, @RequestParam("name") String name) {
        PageResult pageResult = this.abnormal_productsService.abnormallsitpage(pageNum, pageSize, name);
        if (pageResult == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping({"appro"})
    public JsonResult<Object> approvals(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, String name) {
        PageResult pageResult = this.approvalsService.getApprovalslistpage(pageNum, pageSize, name);
        log.info("approvals--------------------------->pageNum:" + pageNum);
        log.info("approvals--------------------------->pageSize:" + pageSize);
        log.info("approvals--------------------------->name:" + name);
        if (pageResult == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping(path = {"addapprovals"}, method = {RequestMethod.POST})
    public JsonResult<Object> addapprovals(Approvals approvals) {
        this.approvalsService.addApprovals(approvals);
        log.info("add================================>approvals"+ approvals);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"listproduct"}, method = {RequestMethod.POST})
    public JsonResult<List<Product>> listproduct() {
        List list = this.productService.list();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"listapproved_by"}, method = {RequestMethod.POST})
    public JsonResult<List<Approved_by>> listapproved_by() {
        List<Approved_by> list = this.approved_byService.listApprovde_by();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"updateapprovals"}, method = {RequestMethod.POST})
    public JsonResult<Void> updateapprovals(Approvals approvals) {
        this.approvalsService.updateApprovals(approvals);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findApprovalsByid/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Approvals> findApprovalsByid(@PathVariable("id") int id) {
        Approvals list = null;
        try {
            list = this.approvalsService.findapprovalsByid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"deleteapprovals/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deleteapprovals(@PathVariable("id") int id) {
        this.approvalsService.deleteapprovalsByid(id);
        log.info(""+ id);
        return new JsonResult(Integer.valueOf(0), "json");
    }
}
